﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class ChietTinhAnUong : System.Web.UI.Page
    {
        protected List<CalculatorInfo> tourMenu = new List<CalculatorInfo>();
        protected decimal TotalMenuPrices = 0;
        protected decimal TotalMenuAdults = 0;
        protected decimal TotalMenuChild = 0;
        protected decimal TotalMenuBaby = 0;
        protected decimal PriceMenuAdults = 0;
        protected decimal PriceMenuChild = 0;
        protected decimal PriceMenuBaby = 0;

        protected List<CalculatorInfo> tourVisa = new List<CalculatorInfo>();
        protected decimal PriceVisaAdults = 0;
        protected decimal PriceVisaChild = 0;
        protected decimal PriceVisaBaby = 0;
        protected decimal TotalVisaPrices = 0;
        protected decimal TotalVisaAdults = 0;
        protected decimal TotalVisaChild = 0;
        protected decimal TotalVisaBaby = 0;

        public List<CalculatorInfo> tourDVK = new List<CalculatorInfo>();
        protected decimal PriceDVKAdults = 0;
        protected decimal PriceDVKChild = 0;
        protected decimal PriceMDVKBaby = 0;
        protected decimal TotalDVKPrices = 0;
        protected decimal TotalDVKAdults = 0;
        protected decimal TotalDVKChild = 0;
        protected decimal TotalDVKBaby = 0;

        public List<CalculatorInfo> tourHH = new List<CalculatorInfo>();
        protected decimal PricesHHAdults = 0;
        protected decimal PricesHHChild = 0;
        protected decimal PricesHHBaby = 0;

        protected decimal TotalHHPrices = 0;
        protected decimal TotalHHAdults = 0;
        protected decimal TotalHHChild = 0;
        protected decimal TotalHHBaby = 0;

        private List<CalculatorInfo> _calculatorList = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorController = new CalculatorController();
        protected TourInfo tourInfo = new TourInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                _calculatorList =
                    _calculatorController.ListCalculator()
                        .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"])).ToList();
                tourInfo = new TourController().GetTour(int.Parse(Request.QueryString["TourId"]));
           
          
            

            }
            LoadAddressDefault();
            LoadNational();
            LoadRestaurantType();
            LoadMenuType();
            //Visa
            LoadPartner();
            LoadVisaType();
            //Dich Vu Khac
            LoadLocationType();
            LoadNationalDVK();

            if (_calculatorList.Count == 0) return;
            RenderForm();
        }
        public void RenderForm()
        {

            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                //Menu
                tourMenu = _calculatorList.Where(p => p.ItemType == 4).ToList();
                PriceMenuAdults = tourMenu.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                PriceMenuChild = tourMenu.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                PriceMenuBaby = tourMenu.Sum(p => ((p.PriceBaby * p.QuantityBaby) + (p.PriceBaby * p.QuantityBaby) * Convert.ToDecimal(p.Vat / 100)));

                TotalMenuAdults = PriceMenuAdults / tourMenu.Sum(p => p.QuantityAdults);
                TotalMenuChild = PriceMenuChild / tourMenu.Sum(p => p.QuantityChild);
                TotalMenuBaby = PriceMenuBaby / tourMenu.Sum(p => p.QuantityBaby);

                //TotalMenuAdults = PriceMenuAdults / tourInfo.QuantityAdult;
                //TotalMenuChild = PriceMenuChild / tourInfo.QuantityChild;
                //TotalMenuBaby = PriceMenuBaby / tourInfo.QuantityBaby;
                TotalMenuPrices = PriceMenuAdults + PriceMenuChild + PriceMenuBaby;

                //dich vu khac
                tourDVK = _calculatorList.Where(p => p.ItemType == 6).ToList();
                PriceDVKAdults = tourDVK.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                PriceDVKChild = tourDVK.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                PriceMDVKBaby = tourDVK.Sum(p => ((p.PriceBaby * p.QuantityBaby) + (p.PriceBaby * p.QuantityBaby) * Convert.ToDecimal(p.Vat / 100)));
                TotalDVKPrices = PriceDVKAdults + PriceDVKChild + PriceMDVKBaby;

                TotalDVKAdults = PriceDVKAdults / tourDVK.Sum(p => p.QuantityAdults);
                TotalDVKChild = PriceDVKChild / tourDVK.Sum(p => p.QuantityChild);
                TotalDVKBaby = PriceMDVKBaby / tourDVK.Sum(p => p.QuantityBaby);

                //TotalDVKAdults = PriceDVKAdults / tourInfo.QuantityAdult;
                //TotalDVKChild = PriceDVKChild / tourInfo.QuantityChild;
                //TotalDVKBaby = PriceMDVKBaby / tourInfo.QuantityBaby;

                //Visa
            

                tourVisa = _calculatorList.Where(p => p.ItemType == 6).ToList();

                PriceVisaAdults = tourVisa.Sum(p => p.TotalPriceVisaAdults);
                PriceVisaChild = tourVisa.Sum(p => p.TotalPriceVisaChild);
                PriceVisaBaby = tourVisa.Sum(p => p.TotalPriceVisaBaby);

                TotalVisaPrices = PriceVisaAdults + PriceVisaChild;
                TotalVisaChild = PriceVisaAdults / tourVisa.Sum(p => p.QuantityAdults);
                TotalVisaAdults = PriceVisaChild / tourVisa.Sum(p => p.QuantityChild);
                TotalVisaBaby = PriceVisaBaby / tourVisa.Sum(p => p.QuantityBaby);

                //TotalVisaChild = PriceVisaAdults / tourInfo.QuantityChild;
                //TotalVisaAdults = PriceVisaChild / tourInfo.QuantityAdult;
                 //TotalVisaBaby = PriceVisaBaby /tourInfo.QuantityBaby ;

                //hoa hoong 
                tourHH = _calculatorList.Where(p => p.ItemType == 11).ToList();
                PricesHHAdults = tourHH.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                PricesHHChild = tourHH.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                PricesHHBaby = tourHH.Sum(p => ((p.PriceBaby * p.QuantityBaby) + (p.PriceBaby * p.QuantityBaby) * Convert.ToDecimal(p.Vat / 100)));
                
                TotalHHPrices = PricesHHAdults + PricesHHChild + PricesHHBaby;
                TotalHHAdults = PricesHHAdults / tourHH.Sum(p => p.QuantityAdults);
                TotalHHChild = PricesHHChild / tourHH.Sum(p => p.QuantityChild);
                TotalHHBaby = PricesHHBaby / tourHH.Sum(p => p.QuantityBaby);
                //TotalHHAdults = PricesHHAdults / tourInfo.QuantityAdult;
                //TotalHHChild = PricesHHChild / tourInfo.QuantityChild;
                //TotalHHBaby = PricesHHBaby / tourInfo.QuantityBaby;
               
            }
        }
        private void LoadAddressDefault()
        {
            ddlNationalMenu.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
            ddlCityMenu.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
            ddlRestaurantMenu.Items.Insert(0, new ListItem("--- Chọn nhà hàng ---", "-1"));
            ddlRestaurantTypeMenu.Items.Insert(0, new ListItem("--- Chọn loại nhà hàng ---", "-1"));
            ddlMenuMenu.Items.Insert(0, new ListItem("--- Chọn thực đơn ---", "-1"));
            ddlMenuTypeMenu.Items.Insert(0, new ListItem("--- Chọn loại thực đơn ---", "-1"));

            ddlVisaNameVisa.Items.Insert(0, new ListItem("--Chọn Visa --", "-1"));
        }
        public void LoadNational()
        {
            ddlNationalMenu.DataSource = new NationalController().ListNational().OrderBy(p => p.NationalName);
            ddlNationalMenu.DataValueField = "NationalId";
            ddlNationalMenu.DataTextField = "NationalName";
            ddlNationalMenu.DataBind();
            ddlNationalMenu.Items.Insert(0, new ListItem("--Chọn quốc gia--", "-1"));
        }

        private void LoadRestaurantType()
        {
            ddlRestaurantTypeMenu.DataSource = new RestaurantTypeController().ListRestaurantType()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.RestaurantTypeName).ToList();
            ddlRestaurantTypeMenu.DataTextField = "RestaurantTypeName";
            ddlRestaurantTypeMenu.DataValueField = "RestaurantTypeId";
            ddlRestaurantTypeMenu.DataBind();
            ddlRestaurantTypeMenu.Items.Insert(0, new ListItem("--- Chọn loại nhà hàng ---", "-1"));
        }
        private void LoadMenuType()
        {
            ddlMenuTypeMenu.DataSource = new MenuTypeController().ListMenuType()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.MenuTypeName);
            ddlMenuTypeMenu.DataTextField = "MenuTypeName";
            ddlMenuTypeMenu.DataValueField = "MenuTypeId";
            ddlMenuTypeMenu.DataBind();
            ddlMenuTypeMenu.Items.Insert(0, new ListItem("--- Chọn loại menu ---", "-1"));
        }
        //Visa
        public void LoadPartner()
        {
            ddlPartnerNameVisa.DataSource = new PartnerController().ListPartner().OrderBy(p => p.PartnerName);
            ddlPartnerNameVisa.DataValueField = "PartnerId";
            ddlPartnerNameVisa.DataTextField = "PartnerName";
            ddlPartnerNameVisa.DataBind();
            ddlPartnerNameVisa.Items.Insert(0, new ListItem("--Chọn nhà cung cấp --", "-1"));
        }
        public void LoadVisaType()
        {
            ddlVisaTypeNameVisa.DataSource = new VisaTypeController().ListVisaType().OrderBy(p => p.VisaTypeName);
            ddlVisaTypeNameVisa.DataValueField = "VisaTypeId";
            ddlVisaTypeNameVisa.DataTextField = "VisaTypeName";
            ddlVisaTypeNameVisa.DataBind();
            ddlVisaTypeNameVisa.Items.Insert(0, new ListItem("--Chọn  loại Visa --", "-1"));
        }

        //Dich vu khac
        public void LoadLocationType()
        {
            ddlLocationTypeDVK.DataSource = new LocationTypeController().ListLocationType().OrderBy(p => p.LocationTypeName);
            ddlLocationTypeDVK.DataValueField = "LocationTypeId";
            ddlLocationTypeDVK.DataTextField = "LocationTypeName";
            ddlLocationTypeDVK.DataBind();
            ddlLocationTypeDVK.Items.Insert(0, new ListItem("--Chọn  loại Visa --", "-1"));
        }

        public void LoadNationalDVK()
        {
            ddlNationalDVK.DataSource = new NationalController().ListNational().OrderBy(p => p.NationalName);
            ddlNationalDVK.DataValueField = "NationalId";
            ddlNationalDVK.DataTextField = "NationalName";
            ddlNationalDVK.DataBind();
            ddlNationalDVK.Items.Insert(0, new ListItem("--Chọn quốc gia--", "-1"));
        }
    }
}